package leilib.ir.crawler.parsers;

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import leilib.ir.crawler.pages.SourcePage;
import leilib.ir.crawler.url.CrawlURL;

import org.junit.Before;
import org.junit.Test;

public class HREFParserTest {
	SourcePage sPage;

	HREFParser parser = new HREFParser();
	CrawlURL cURL;

	@Before
	public void setUp() throws Exception {

		// FileReader fr = new FileReader(new File("E:/text.html"));
		// String content

		// URL url = new URL("http://robot.scut.edu.cn");
		cURL = new CrawlURL("http://www.google.com/");

		URL url = cURL.getUrl();
		URLConnection c = url.openConnection();

		InputStreamReader r = new InputStreamReader(c.getInputStream());
		BufferedReader br = new BufferedReader(r);

		String content = null;
		content = br.readLine();

		StringBuffer sb = new StringBuffer();
		while (content != null) {
			sb.append(content + "\n");
			content = br.readLine();
		}
		System.out.println(sb.toString());

		sPage = new SourcePage(sb.toString(), cURL);
	}

	@Test
	public void testParser() {
		ArrayList<CrawlURL> curls = this.parser.parser(this.sPage);

		assertNotNull(curls);
		assertTrue(curls.size() != 0);
	}

//	@Test
//	public void testDealWithCurrentDiretory() {
//
//		String base = "http://www.baidu.com:80/dir1/dir2/";
//		String rela = "./file.txt";
//		String result = this.parser.dealWithCurrentDiretory(base, rela);
//
//		assertEquals("http://www.baidu.com:80/dir1/dir2/file.txt", result);
//	}
//
//	@Test
//	public void testExtractAbsoluteDiretory() {
//		String base = "/dir1/dir2/";
//		String rela = "/file.txt";
//		String result = this.parser.extractAbsoluteDiretory(base, rela);
//
//		assertEquals("/dir1/dir2/file.txt", result);
//	}
//
//	@Test
//	public void testDealWithParentDiretory() {
//		String base = "/dir1/dir2/";
//		String rela = "../file.txt";
//		String result = this.parser.dealWithParentDiretory(base, rela);
//
//		assertEquals("/dir1/file.txt", result);
//
//		base = "/dir1/dir2/";
//		rela = "../../file.txt";
//		result = this.parser.dealWithParentDiretory(base, rela);
//
//		assertEquals("/file.txt", result);
//
//		base = "/dir1/dir2/";
//		rela = ".././../file.txt";
//		result = this.parser.dealWithParentDiretory(base, rela);
//
//		assertEquals("/file.txt", result);
//
//	}

}
